package com.cj.bus.controller;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cj.bus.common.entity.Constents;
import com.cj.bus.common.entity.ResponseEntity;
import com.cj.bus.common.entity.ResponseHelper;
import com.cj.bus.common.entity.ValidationGroups;
import com.cj.bus.common.utils.RequestUtils;
import com.cj.bus.entity.BusResourceEntity;
import com.cj.bus.service.BusResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/resourceComment")
@Validated
@Api(tags = {"资源相关接口"})
@Slf4j
public class BusResourceCommentController {

	@Autowired private BusResourceService busResourceService;
	
	
	@SuppressWarnings({ "unchecked"})
	@GetMapping("/list")
	@ApiOperation(value = "查询所有的资源")
	public ResponseEntity<List<BusResourceEntity>> list(
			Integer page,
			String keys,
			String types,
			String orderField,
			String orderType) {
		
		PageHelper.startPage(null == page ? 1 : page, Constents.DEFAULT_ROWS);
		if(StringUtils.isBlank(orderField)) {
			PageHelper.orderBy("t.resource_id desc");
		}else {
			PageHelper.orderBy("t." + orderField + " " + null == orderType?"desc":"asc");
		}
		List<Integer> typesInt= null;
		if(StringUtils.isNotBlank(types)) {
		   typesInt= Arrays.asList(types .split(",")).stream().map(s -> (Integer.parseInt(s.trim()))).collect(Collectors.toList());
		}
		PageInfo<BusResourceEntity> list = (PageInfo<BusResourceEntity>)busResourceService.selectList(keys, typesInt, Constents.RESOURCE_PUB_SUCCESS_STRING);
		if(null == list || CollectionUtils.isEmpty(list.getList())) {
			return ResponseHelper.success(new ArrayList<>());
		}
		return ResponseHelper.success(list.getList());
	}
	
	@GetMapping("/find/{resourceId}")
	@ApiOperation(value = "查询单个资源")
	public ResponseEntity<BusResourceEntity> findOne(@PathVariable("resourceId") Long resourceId) {
		Assert.notNull(resourceId,"资源ID未输入");
		BusResourceEntity resource = this.busResourceService.getById(resourceId);
		return ResponseHelper.success(resource);
	}
	
	@PostMapping("/push")
	@ApiOperation(value = "发布资源")
	public ResponseEntity<Long> push(
			@Validated({ValidationGroups.Add.class}) 
	        @RequestBody BusResourceEntity record) {
		record.setCreateTime(LocalDateTime.now());
		if(null == record.getPublishUserId()) {
			record.setPublishUserId(RequestUtils.getLoginId());
			record.setPublishDeptId(RequestUtils.getLoginDeptId());
			record.setPublishUserName(RequestUtils.getLoginName());
		}
		record.setCommentNum(0);
		record.setStatus(Constents.RESOURCE_PUB_INIT_STRING);
		record.setStoreNum(0);
		record.setBrowseNum(0);
		this.busResourceService.save(record);
		return ResponseHelper.success(record.getResourceId());
	}


}
